﻿using Asion.Dapper.Model;
using Dapper;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Text;

namespace Asion.Dapper
{

    /// <summary>
    /// 连表查询
    /// </summary>
    public interface IJoinQueryRepository
    {
        /// <summary>
        /// 查询
        /// </summary>
        /// <typeparam name="TEntity"></typeparam>
        /// <param name="sql"></param>
        /// <param name="tenant"></param>
        /// <returns></returns>
        public IEnumerable<TEntity> Query<TEntity>(string tenant, string sql, object param = null);


        /// <summary>
        /// 查询
        /// </summary>
        /// <typeparam name="TEntity"></typeparam>
        /// <param name="sql"></param>
        /// <param name="tenant"></param>
        /// <returns></returns>
        public TEntity QueryFirstOrDefault<TEntity>(string tenant, string sql, object param = null);

        /// <summary>
        /// 查询
        /// </summary>
        /// <typeparam name="TEntity"></typeparam>
        /// <param name="sql"></param>
        /// <param name="tenant"></param>
        /// <returns></returns>
        public IEnumerable<TReturn> Query<TFirst, TSecond, TReturn>(string tenant, string sql, Func<TFirst, TSecond, TReturn> map, string splitOn, object param = null);

        /// <summary>
        /// 查询
        /// </summary>
        /// <typeparam name="TEntity"></typeparam>
        /// <param name="sql"></param>
        /// <param name="tenant"></param>
        /// <returns></returns>
        public PageReponse<TEntity> PageQuery<TEntity>(string tenant, string selectsSql, string countSql, object param = null, int pageNumber = 1, int rowsPerPage = DapperConfiguration.default_rows_page);


        /// <summary>
        /// 查询
        /// </summary>
        /// <typeparam name="TEntity"></typeparam>
        /// <param name="sql"></param>
        /// <param name="tenant"></param>
        /// <returns></returns>
        public PageReponse<TEntity> PageQuery<TEntity>(string tenant, JoinPageQueryConditionRequest request);
    }

    public class SimpleJoinQueryRepositroy : IJoinQueryRepository
    {
        public IEnumerable<TEntity> Query<TEntity>(string tenant, string sql, object param = null)
        {
            using (var connection = DapperConfiguration.connection_factory.CreateConnection(tenant))
            {
                return connection.Query<TEntity>(sql, param: param);
            }
        }


        public IEnumerable<TReturn> Query<TFirst, TSecond, TReturn>(string tenant, string sql, Func<TFirst, TSecond, TReturn> map, string splitOn, object param = null)
        {
            using (var connection = DapperConfiguration.connection_factory.CreateConnection(tenant))
            {
                return connection.Query<TFirst, TSecond, TReturn>(sql, map, param: param, splitOn: splitOn);
            }
        }

        public PageReponse<TEntity> PageQuery<TEntity>(string tenant, string selectsSql, string countSql, object param = null, int pageNumber = 1, int rowsPerPage = DapperConfiguration.default_rows_page)
        {
            IEnumerable<TEntity> resultA;
            int resultB = 0;
            string sql = selectsSql.Contains(";") ? selectsSql : selectsSql + ";";
            sql += countSql;
            using (var connection = DapperConfiguration.connection_factory.CreateConnection(tenant))
            {
                var girdReaderResult = connection.QueryMultiple(sql, param);
                resultA = girdReaderResult.Read<TEntity>();
                resultB = girdReaderResult.ReadFirst<int>();
            }

            return new PageReponse<TEntity>(pageNumber, rowsPerPage, resultB, resultA);
        }

        public PageReponse<TEntity> PageQuery<TEntity>(string tenant, JoinPageQueryConditionRequest request)
        {
            IEnumerable<TEntity> resultA;
            int resultB = 0;
            var sqls = request.BuildSql();
            var sql = sqls.Item1 + ";" + sqls.Item2;
            if (Debugger.IsAttached)
                Trace.WriteLine(request.PrintSql());
            using (var connection = DapperConfiguration.connection_factory.CreateConnection(tenant))
            {
                var girdReaderResult = connection.QueryMultiple(sql, request.Parameter);
                resultA = girdReaderResult.Read<TEntity>();
                resultB = girdReaderResult.ReadFirst<int>();
            }

            return new PageReponse<TEntity>(request.PageNumber, request.RowsPerPage, resultB, resultA);
        }

        public TEntity QueryFirstOrDefault<TEntity>(string tenant, string sql, object param = null)
        {
            using (var connection = DapperConfiguration.connection_factory.CreateConnection(tenant))
            {
                return connection.QueryFirstOrDefault<TEntity>(sql, param: param);
            }
        }
    }
}
